import wechat from 'weixin-js-sdk'
// import request from '@/utils/request'
import dao from '@/utils/daoji';
const appId = 'wx4a6a34e871b99bcb'

function config (callback) {
  const href = window.location.href
  dao({
    url: `/api/wechat/getWechatSign`,
    method: 'post',
    data: {
      url: href
    },
    hideLoading: 1
  }).then(res => {
    callback(res.data.data)
  })
}

export function wekupApp (callback) {
  config(res => {
    wechat.config({
      // debug: true, // 开启调试模式
      appId, // 必填，公众号的唯一标识
      timestamp: res.timestamp, // 必填，生成签名的时间戳
      nonceStr: res.noncestr, // 必填，生成签名的随机串
      signature: res.signature, // 必填，签名，见附录1
      jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline', 'wx-open-launch-app'],
      openTagList: ['wx-open-launch-app']
    })
  })

  wechat.ready(function () {
    callback()
  })

  wechat.error(function (res) {
    console.log(res)
  })
}

export function share (shareConfig) {
  console.log(shareConfig);
  // console.log(123);
  config(res => {
    // console.log(res);
    wechat.config({
      // debug: true, // 开启调试模式
      appId, // 必填，公众号的唯一标识
      timestamp: res.timestamp, // 必填，生成签名的时间戳
      nonceStr: res.noncestr, // 必填，生成签名的随机串
      signature: res.signature, // 必填，签名，见附录1
      jsApiList: ['onMenuShareAppMessage', 'updateAppMessageShareData', 'onMenuShareTimeline'],
      // openTagList: ['wx-open-launch-app']
    })
  })

  wechat.ready(function () {
    wechat.updateAppMessageShareData({
      title: shareConfig.title, // 分享标题
      desc: shareConfig.desc, // 分享描述
      link: shareConfig.shareUrl, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      imgUrl: shareConfig.imgUrl, // 分享图标
      success: function () {
        console.log('分享配置成功')
      }
    })

    wechat.onMenuShareTimeline({
      title: shareConfig.title, // 分享标题
      desc: shareConfig.desc, // 分享描述
      link: shareConfig.shareUrl, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      imgUrl: shareConfig.imgUrl, // 分享图标
      success: function () {
        console.log('分享配置成功')
      }
    })
  })

  wechat.error(function (res) {
    console.log(res)
  })
}
